package com.stu.student_back_end.controller;

import cn.hutool.core.util.StrUtil;
import com.stu.student_back_end.common.Constants;
import com.stu.student_back_end.common.Result;
import com.stu.student_back_end.controller.dto.AccountDTO;
import com.stu.student_back_end.controller.dto.PageDTO;
import com.stu.student_back_end.entity.TblAccount;
import com.stu.student_back_end.service.ITblAccountService;
import com.stu.student_back_end.service.ITblStudentService;
import com.stu.student_back_end.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 登录页面操作
 */

@RestController
public class AccountController {

    @Autowired
    private ITblAccountService accountService;

    @Autowired
    private ITblStudentService studentService;

    @GetMapping("/login")
    public Result loginAccount(AccountDTO accountDTO, HttpServletRequest request) {
        String front_token = request.getHeader("Authorization");
        if(!(front_token == null)){
            if(TokenUtil.verifyToken(front_token)){
                return Result.success();
            }
        }
        String username = accountDTO.getUsername();
        String password = accountDTO.getPassword();
        if(StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            return Result.error(Constants.CODE_400,"参数错误");
        }
        if(accountDTO.getUserType().equals("学生")) {
            return studentService.login(username,password);
        }else {
            return accountService.login(accountDTO);
        }
    }

    /**
     * 添加宿管数据
     * @param account
     * @return
     */
    @PostMapping("/add")
    public Result addAccount(@RequestBody TblAccount account) {
        Boolean flag = accountService.addAccount(account);
        if(!flag) return Result.error(Constants.CODE_500,"添加宿管信息失败！");
        return Result.success("添加宿管信息成功!");
    }

    /**
     * 分页查询宿管信息
     * @param pageDTO
     * @return
     */
    @PostMapping("/a/query")
    public Result queryAccount(@RequestBody PageDTO pageDTO) {
        return Result.success(accountService.queryAccount(pageDTO));
    }


    /**
     * 根据id查询修改的宿管表单数据
     * @param id 宿管表单id
     * @return
     */
    @PostMapping("/update/{id}")
    public Result queryByIdAccount(@PathVariable("id") int id) {
        TblAccount account = accountService.getById(id);
        if(account == null) {
            return Result.error(Constants.CODE_500,"加载失败!");
        }else {
            return Result.success("加载成功！",account);
        }
    }

    /**
     * 修改宿管信息
     * @param account
     * @return
     */
    @PostMapping("/update")
    public Result updateAccount(@RequestBody TblAccount account) {
        boolean update = accountService.updateById(account);
        if(!update) {
            return Result.error(Constants.CODE_600,"修改宿管信息失败！");
        }else {
            return Result.success("修改宿管信息成功！");
        }
    }

    /**
     * 根据id删除宿管数据
     * @param id
     * @return
     */
    @DeleteMapping("/del/{id}/{name}")
    public Result delAccount(@PathVariable("id") int id,@PathVariable("name") String name) {
        if(!accountService.delAccountAndBuilding(id,name)) return Result.error(Constants.CODE_500,"删除失败！");
        return Result.success("删除成功");
    }

    /**
     * 根据id修改指定id的密码
     * @param id
     * @param password
     * @return
     */
    @PostMapping("/password/{id}/{password}")
    public Result updatePassWord(@PathVariable("id") int id,@PathVariable("password") String password){
        if(password == null) return Result.error(Constants.CODE_400,"参数为空!");
        TblAccount account = accountService.getById(id);
        account.setPassword(password);
        return Result.success(accountService.updateById(account));
    }

    /**
     * 查询所有的数据
     * @return
     */
    @GetMapping("/queryAll")
    public Result queryAll() {
        return Result.success(accountService.list());
    }
}
